{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import MalardHelpers\n",
    "import DataSetQuery\n",
    "import datetime\n",
    "\n",
    "#Sets the URL of the Malard ServiceGateway.\n",
    "query = DataSetQuery.DataSetQuery('http://localhost:9000','DEV')\n",
    "\n",
    "#Setup the bounding box\n",
    "minX=1300000\n",
    "maxX=1400000\n",
    "minY=0\n",
    "maxY=100000\n",
    "minT=datetime.datetime(2012,1,1,0,0)\n",
    "maxT=datetime.datetime(2012,3,31,0,0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## TANDEMX\n",
    "#Returns a file handle to a NetCDF that is a merge and filter of all the shards in a BoundingBox.\n",
    "fileNameTandemX = query.getNetCdfFile('mtngla','tandemx',minX,maxX,minY,maxY,minT,maxT)\n",
    "\n",
    "print(\"File created: %s\" % (fileNameTandemX))\n",
    "\n",
    "#Converts the NetCDF into a DataFrame.\n",
    "dfTandemX = MalardHelpers.getDataFrameFromNetCDF(fileNameTandemX)\n",
    "\n",
    "print(\"TandemX Max Elevation %f\" % (dfTandemX['elev'].max()))\n",
    "print(\"TandemX Min Elevation %f\" % (dfTandemX['elev'].min()))\n",
    "print(\"TandemX Count Elevation %f\" % (dfTandemX['elev'].count()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## SRTM\n",
    "#Returns a file handle to a NetCDF that is a merge and filter of all the shards in a BoundingBox.\n",
    "fileNameSrtm = query.getNetCdfFile('mtngla','srtm',minX,maxX,minY,maxY,minT,maxT)\n",
    "\n",
    "print(\"File created: %s\" % (fileNameSrtm))\n",
    "\n",
    "#Converts the NetCDF into a DataFrame.\n",
    "dfSrtm = MalardHelpers.getDataFrameFromNetCDF(fileNameSrtm)\n",
    "\n",
    "print(\"SRTM Max Elevation %f\" % (dfSrtm['elev'].max()))\n",
    "print(\"SRTM Min Elevation %f\" % (dfSrtm['elev'].min()))\n",
    "print(\"SRTM Count Elevation %f\" % (dfSrtm['elev'].count()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Filter criteria\n",
    "\n",
    "#a list of columns to see in the output (x,y,time will be added)\n",
    "projections = ['elev','power','coh'] \n",
    "#a list of dictionaries that specify the filters\n",
    "#only numeric fields are supported with the following operations gt(greater than), gte ( greater than equals), lt (less than) and lte (less than equals)\n",
    "#filters are treated as and conditions\n",
    "filters = [{'column':'power','op':'gt','threshold':10000},{'column':'coh','op':'gt','threshold':0.8}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# TANDEMX\n",
    "\n",
    "#No filters\n",
    "nofiltersTandemX = query.executeQuery( 'mtngla','tandemx',minX,maxX,minY,maxY,minT,maxT,projections,[])\n",
    "#Converts the NetCDF into a DataFrame.\n",
    "dfNoFilterTandemX = MalardHelpers.getDataFrameFromNetCDF(nofiltersTandemX)\n",
    "\n",
    "print( \"TandemX Max Coh [%f] Min Coh [%f] Max Power [%f] Min Power [%f] Count [%d]\" % ( dfNoFilterTandemX['coh'].max(),  dfNoFilterTandemX['coh'].min(), dfNoFilterTandemX['power'].max(), dfNoFilterTandemX['power'].min(), dfNoFilterTandemX['power'].count() ) )  \n",
    "\n",
    "#With filters\n",
    "withfiltersTandemX = query.executeQuery( 'mtngla','tandemx',minX,maxX,minY,maxY,minT,maxT,projections,filters)\n",
    "#Converts the NetCDF into a DataFrame.\n",
    "withFilterTandemX = MalardHelpers.getDataFrameFromNetCDF(withfiltersTandemX)\n",
    "\n",
    "print( \"TandemX Max Coh [%f] Min Coh [%f] Max Power [%f] Min Power [%f] Count [%d]\" % ( withFilterTandemX['coh'].max(),  withFilterTandemX['coh'].min(), withFilterTandemX['power'].max(), withFilterTandemX['power'].min(), withFilterTandemX['power'].count() ) )  \n",
    "print( \"TandemX Max Lon [%f] Min Coh [%f] Max Power [%f] Min Power [%f] Count [%d]\" % ( withFilterTandemX['x'].max(),  withFilterTandemX['coh'].min(), withFilterTandemX['power'].max(), withFilterTandemX['power'].min(), withFilterTandemX['power'].count() ) )  \n",
    "print( \"TandemX Min Time [%f] Max Time [%f]\" % ( withFilterTandemX['time'].min(),  withFilterTandemX['time'].max() ) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# SRTM\n",
    "\n",
    "#No filters\n",
    "nofiltersSrtm = query.executeQuery( 'mtngla','srtm',minX,maxX,minY,maxY,minT,maxT,projections,[])\n",
    "#Converts the NetCDF into a DataFrame.\n",
    "dfNoFilterSrtm = MalardHelpers.getDataFrameFromNetCDF(nofiltersSrtm)\n",
    "\n",
    "print( \"SRTM Max Coh [%f] Min Coh [%f] Max Power [%f] Min Power [%f] Count [%d]\" % ( dfNoFilterSrtm['coh'].max(),  dfNoFilterSrtm['coh'].min(), dfNoFilterSrtm['power'].max(), dfNoFilterSrtm['power'].min(), dfNoFilterSrtm['power'].count() ) )  \n",
    "\n",
    "#With filters\n",
    "withfiltersSrtm = query.executeQuery( 'mtngla','srtm',minX,maxX,minY,maxY,minT,maxT,projections,filters)\n",
    "#Converts the NetCDF into a DataFrame.\n",
    "withFilterSrtm = MalardHelpers.getDataFrameFromNetCDF(withfiltersSrtm)\n",
    "\n",
    "print( \"SRTM Max Coh [%f] Min Coh [%f] Max Power [%f] Min Power [%f] Count [%d]\" % ( withFilterSrtm['coh'].max(),  withFilterSrtm['coh'].min(), withFilterSrtm['power'].max(), withFilterSrtm['power'].min(), withFilterSrtm['power'].count() ) )  \n",
    "print( \"SRTM Max Lon [%f] Min Coh [%f] Max Power [%f] Min Power [%f] Count [%d]\" % ( withFilterSrtm['x'].max(),  withFilterSrtm['coh'].min(), withFilterSrtm['power'].max(), withFilterSrtm['power'].min(), withFilterSrtm['power'].count() ) )  \n",
    "print( \"SRTM Min Time [%f] Max Time [%f]\" % ( withFilterSrtm['time'].min(),  withFilterSrtm['time'].max() ) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "## SRTM plot with no filters\n",
    "plt.figure(figsize=(12,10))\n",
    "plt.scatter(x=dfNoFilterSrtm['x'], y=dfNoFilterSrtm['y'], c=dfNoFilterSrtm['elev'], marker='.', s=20)\n",
    "plt.colorbar()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## SRTM plot with filters\n",
    "plt.figure(figsize=(12,10))\n",
    "#vmin and vmax set min and max of colour bar\n",
    "plt.scatter(x=withFilterSrtm['x'], y=withFilterSrtm['y'], c=withFilterSrtm['elev'], marker='.', s=20, vmin=dfNoFilterSrtm['elev'].min(), vmax=dfNoFilterSrtm['elev'].max())\n",
    "plt.colorbar()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "from bokeh.plotting import figure, output_file\n",
    "from bokeh.io import output_notebook, push_notebook, show\n",
    "from bokeh.layouts import row, gridplot\n",
    "from bokeh.models import ColumnDataSource, ColorBar, LinearColorMapper, LogColorMapper\n",
    "from bokeh.palettes import Plasma8\n",
    "\n",
    "# output to static HTML file\n",
    "# output_file(\"TandemX_vs_SRTM_1300000_1400000_0_100000_1325376000000_1333148400000.html\")\n",
    "\n",
    "# output to notebook (below)\n",
    "output_notebook()\n",
    "\n",
    "# define data sources\n",
    "sourceTandemX = ColumnDataSource(dict(x=dfNoFilterTandemX['x'],y=dfNoFilterTandemX['y'], elev=dfNoFilterTandemX['elev']))\n",
    "sourceSrtm = ColumnDataSource(dict(x=dfNoFilterSrtm['x'],y=dfNoFilterSrtm['y'], elev=dfNoFilterSrtm['elev']))\n",
    "\n",
    "# Mapper\n",
    "#mapperLin=LinearColorMapper(palette=Plasma8, low=withFilterTandemX['elev'].min(), high=withFilterTandemX['elev'].max())\n",
    "mapperLog=LogColorMapper(palette=Plasma8, low=withFilterTandemX['elev'].min(), high=dfNoFilterTandemX['elev'].max())\n",
    "tools = \"pan,wheel_zoom,zoom_in,zoom_out,box_zoom,box_select,hover,reset,\"\n",
    "\n",
    "# define tooltip attributes\n",
    "tooltips = [\n",
    "    (\"(x,y)\", \"(@x{0}, @y{0})\"),\n",
    "    (\"elevation\", \"@elev{0.00}\"),\n",
    "]\n",
    "\n",
    "# create a TandemX plot\n",
    "p1 = figure(plot_width=700, plot_height=550, title=\"TandemX\", x_axis_label='x', y_axis_label='y', tools=tools, tooltips=tooltips)\n",
    "p1.scatter(x='x', y='y', fill_alpha=0.6, line_color=None, fill_color={'field': 'elev', 'transform': mapperLog}, source=sourceTandemX)                     \n",
    "\n",
    "# create a SRTM plot\n",
    "p2 = figure(plot_width=700, plot_height=550, title=\"SRTM\", x_axis_label='x', y_axis_label='y', tools=tools, tooltips=tooltips, x_range=p1.x_range, y_range=p1.y_range)\n",
    "p2.scatter(x='x', y='y', fill_alpha=0.6, line_color=None, fill_color={'field': 'elev', 'transform': mapperLog}, source=sourceSrtm)\n",
    "\n",
    "# Add colorbar\n",
    "color_bar = ColorBar(color_mapper=mapperLog, width=8, location=(0,0))\n",
    "p1.add_layout(color_bar, 'right')   \n",
    "\n",
    "# show plots\n",
    "show(gridplot([[p1],[p2]]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
